//
//  V2NodeContentViewController.m
//  V2EX
//
//  Created by CYT on 16/6/13.
//  Copyright © 2016年 Chris. All rights reserved.
//

#import "V2NodeContentViewController.h"
#import "V2TopicToolBarItemView.h"
#import <MJRefresh.h>

@interface V2NodeContentViewController ()
/**
 *  menu
 */
@property (nonatomic, strong) UIView          *menuContainView;
@property (nonatomic, strong) UIView          *menuView;
@property (nonatomic, strong) UIButton        *menuBackgroundButton;
@property (nonatomic, assign) BOOL isMenuShowing;
@end

@implementation V2NodeContentViewController

#pragma mark - <Setup>
- (instancetype)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:style]) {
//        self.pageCount = 1;
        self.isMenuShowing = NO;
//        self.needsRefresh = YES;
        
    }
    return self;
}


- (void)loadView {
    [super loadView];
    
    [self setupNavItems];
    [self setupTableView];
    [self setupMenuView];
    [self setupMJRefresh];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = self.model.nodeTitle;
    
}


- (void)setupNavItems {
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem gk_itemWithImage:[UIImage imageNamed:@"navi_back"] highImage:nil target:self action:@selector(barItemClickBack)];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem gk_itemWithImage:[UIImage imageNamed:@"navi_add"] highImage:nil target:self action:@selector(barItemClickMenu)];
    
}

- (void)setupTableView {
    self.tableView.frame           = [UIScreen mainScreen].bounds;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate        = self;
    self.tableView.dataSource      = self;
    
}


- (void)setupMenuView {
    
    self.menuContainView = [[UIView alloc] init];
    self.menuContainView.userInteractionEnabled = NO;
    [self.view addSubview:self.menuContainView];
    
    self.menuBackgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.menuBackgroundButton.backgroundColor = [UIColor colorWithWhite:0.667 alpha:0];
    [self.menuBackgroundButton addTarget:self action:@selector(menuBackgroundButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.menuContainView addSubview:self.menuBackgroundButton];
    //手势不知道有什么意义
//    UIPanGestureRecognizer *menuBGButtonPanGesture = [UIPanGestureRecognizer bk_recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
//        @strongify(self)
//        [self hideMenuAnimated:NO];
//    }];
//    [self.menuBackgroundButton addGestureRecognizer:menuBGButtonPanGesture];
    
    
    self.menuView = [[UIView alloc] init];
    self.menuView.alpha = 0.0;
    self.menuView.frame = (CGRect){200, 64, 130, 118};
    [self.menuContainView addSubview:self.menuView];
    
    UIView *topArrowView = [[UIView alloc] init];
    topArrowView.frame = (CGRect){87, 5, 10, 10};
    topArrowView.backgroundColor = [UIColor blackColor];
    topArrowView.transform = CGAffineTransformMakeRotation(M_PI_4);
    [self.menuView addSubview:topArrowView];
    
    UIView *menuBackgroundView = [[UIView alloc] init];
    menuBackgroundView.frame = (CGRect){10, 10, 100, 88};
    menuBackgroundView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.90];
    menuBackgroundView.layer.cornerRadius = 5.0;
    menuBackgroundView.clipsToBounds = YES;
    [self.menuView addSubview:menuBackgroundView];
    
    NSArray *itemTitleArray = @[@"发帖", @"收藏"];
    NSArray *itemImageArray = @[@"icon_post", @"icon_fav"];
    
    
    for (int i = 0; i < 2; i ++) {
        V2TopicToolBarItemView *item = [[V2TopicToolBarItemView alloc] init];
        item.itemTitle = itemTitleArray[i];
        item.itemImage = [UIImage imageNamed:itemImageArray[i]];
        item.alpha = 1.0;
        item.frame = (CGRect){0, 44 * i, item.width, item.height};
        item.backgroundColorNormal = [UIColor clearColor];
        [menuBackgroundView addSubview:item];
    }
}


- (void)setupMJRefresh
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewDatas)];
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    // 默认
    [self.tableView.mj_header beginRefreshing];
}


- (void)loadNewDatas
{
    [self.tableView.mj_header endRefreshing];
    
}

#pragma mark - Layout

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.menuContainView.frame = self.view.bounds;
    self.menuBackgroundButton.frame = self.menuContainView.bounds;
    
}



#pragma mark - <Private Method>
- (void)menuBackgroundButtonClick
{
    [self hideMenuAnimated:NO];
}


- (void)barItemClickBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)barItemClickMenu
{
    if (self.isMenuShowing) {
        [self hideMenuAnimated:YES];
    }
    else {
        [self showMenuAnimated:YES];
    }
}

- (void)showMenuAnimated:(BOOL)animated {
    
    if (self.isMenuShowing) {
        return;
    }
    
    self.isMenuShowing = YES;
    
    
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    CGRect rect = (CGRect){360, 15, 50, 50};
    CGRect addBarF = [self.menuContainView convertRect:rect fromView:window];
    self.menuView.frame = (CGRect){CGRectGetMidX(addBarF) - 72, CGRectGetMaxY(addBarF), 130, 118};
    
    if (animated) {
        self.menuView.origin = (CGPoint){CGRectGetMidX(addBarF) - 72, CGRectGetMaxY(addBarF) - 44};
        //        self.menuView.origin = (CGPoint){220, 20};
        self.menuView.transform = CGAffineTransformMakeScale(0.3, 0.3);
        
        [UIView animateWithDuration:0.3 animations:^{
            self.menuView.alpha = 1.0;
            self.menuView.transform = CGAffineTransformIdentity;
            self.menuView.frame = (CGRect){CGRectGetMidX(addBarF) - 92, CGRectGetMaxY(addBarF), 130, 118};
            //            self.menuView.frame = (CGRect){200, 64, 130, 118};
        } completion:^(BOOL finished) {
            self.menuContainView.userInteractionEnabled = YES;
        }];
    } else {
        self.menuView.alpha = 1.0;
        self.menuView.transform = CGAffineTransformIdentity;
        self.menuView.frame = (CGRect){CGRectGetMidX(addBarF) - 72, CGRectGetMaxY(addBarF), 130, 118};
        //        self.menuView.frame = (CGRect){200, 64, 130, 118};
        self.menuContainView.userInteractionEnabled = YES;
    }
    
}

- (void)hideMenuAnimated:(BOOL)animated {
    
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    CGRect addBarF = [self.menuContainView convertRect:self.navigationItem.rightBarButtonItem.customView.frame fromView:window];
    
    if (animated) {
        [UIView animateWithDuration:0.2 animations:^{
            self.menuView.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.menuContainView.userInteractionEnabled = NO;
        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.menuView.transform = CGAffineTransformMakeScale(0.3, 0.3);
            self.menuView.origin = (CGPoint){CGRectGetMidX(addBarF) - 72 + 40, CGRectGetMaxY(addBarF)};
            //            self.menuView.origin = (CGPoint){260, 64};
            
        } completion:^(BOOL finished) {
            self.menuView.transform = CGAffineTransformIdentity;
            self.menuView.frame = (CGRect){CGRectGetMidX(addBarF) - 72, CGRectGetMaxY(addBarF), 130, 118};
            //            self.menuView.frame = (CGRect){200, 64, 130, 118};
            self.isMenuShowing = NO;
        }];
    } else {
        [UIView animateWithDuration:0.1 animations:^{
            self.menuView.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.menuContainView.userInteractionEnabled = NO;
            self.isMenuShowing = NO;
        }];
    }
    
}

#pragma mark - TableViewDelegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self hideMenuAnimated:YES];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@------%zd",self.model.nodeTitle,indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    V2TopicModel *model = self.topicList.list[indexPath.row];
//    V2TopicViewController *topicViewController = [[V2TopicViewController alloc] init];
//    topicViewController.model = model;
//    [self.navigationController pushViewController:topicViewController animated:YES];
//    
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
